package cn.edu.cug.cs.gtl.utils;

import cn.edu.cug.cs.gtl.protos.DataKey;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DatabaseUtil {

    // 数据库URL，通常是jdbc:postgresql://<host>:<port>/<database_name>
    private static final String DATABASE_URL = "jdbc:postgresql://139.9.45.0:5432/geospacex?charSet=UTF-8";
    // 数据库用户
    private static final String USER = "gis";
    // 数据库密码
    private static final String PASS = "gis";

    static {
        try {
            // 加载PostgreSQL数据库驱动类
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("Cannot find the driver in the classpath!", e);
        }
    }

    public static Connection getConnection() throws SQLException {
        // 创建数据库连接
        return DriverManager.getConnection(DATABASE_URL, USER, PASS);
    }

//    // 将对象转换为字符串
//    public static byte[] serializeToString(Object dataKey) throws IOException {
//        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
//             ObjectOutputStream oos = new ObjectOutputStream(baos)) {
//            oos.writeObject(dataKey);
//            return baos.toByteArray();
//        }
//    }
//
//    public static DataKey deserializeFromString(byte[] key) throws Exception {
//        try (ByteArrayInputStream bais = new ByteArrayInputStream(key);
//             ObjectInputStream ois = new ObjectInputStream(bais)) {
//            return (DataKey) ois.readObject();
//        }
//    }
}

